package cn.jinzhu.hystrix.controller;

import cn.jinzhu.entity.User;
import cn.jinzhu.hystrix.HelloCommand;
import cn.jinzhu.hystrix.service.HelloService;

import cn.jinzhu.hystrix.service.UserCollpaseCommond;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class HellorController {
    @Autowired
    HelloService helloService;
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/hello")
    public String hello(){//服务降级
        return  helloService.hello();
    }
    @GetMapping("/hello1")//请求命令 就是替代服务降级，就是替代注解方式，以为服务降级是以注解的方式进行断路，请求命令就是继承hystrixCommond
    public void hello1() throws ExecutionException, InterruptedException {
        //        二选一，一个实例只能执行一次
//        HelloCommand jinzhu = new HelloCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("jinzhu")),restTemplate);
//        String execute = jinzhu.execute();
//        System.out.println(execute); //直接执行  同步调用
//
////        二选一，一个实例只能执行一次
//        Future<String> queue = jinzhu.queue();
//        String s = queue.get();
//        System.out.println(s);//先入队后执行  异步调用  比上面的好

    }
    //以注解的方式实现异步调用  也叫做号外
    @GetMapping("/hell2")
    public void hello2() throws ExecutionException, InterruptedException {//服务降级
        Future<String> stringFuture = helloService.hello1();
        System.out.println( stringFuture.get());
    }

    @GetMapping("/hello3")

    public void hello3(){//服务降级
        //开启缓存，初始化缓存
        HystrixRequestContext context =  HystrixRequestContext.initializeContext();
       String s= helloService.hello2("金柱");
         helloService.hello3("金柱");//删除数据同时也会删除缓存里面的数据
         s= helloService.hello2("金柱");//删除了缓存数据，还是会收到请求
        context.close();   //缓存结束

    }

    @GetMapping("/hello4") //hystrix请求合并和分发，继承
    public void hello4() throws ExecutionException, InterruptedException {
       HystrixRequestContext c= HystrixRequestContext.initializeContext();//初始上下文
        UserCollpaseCommond cmd1 = new UserCollpaseCommond(helloService,11);
        UserCollpaseCommond cmd2 = new UserCollpaseCommond(helloService,12);
        UserCollpaseCommond cmd3 = new UserCollpaseCommond(helloService,13);
        Future<User> queue1 = cmd1.queue();
        Future<User> queue2 = cmd2.queue();
        Future<User> queue3 = cmd3.queue();
        User u1 = queue1.get();
        User u2 = queue2.get();
        User u3= queue3.get();
        System.out.println(u1);
        System.out.println(u2);
        System.out.println(u3);
        c.close();//结束初始化

    }

    @GetMapping("/hello5") //hystrix请求合并和分发 注解的方式
    public void hello5() throws ExecutionException, InterruptedException {
        HystrixRequestContext c= HystrixRequestContext.initializeContext();//初始上下文
        Future<User> queue1 = helloService.getUserById(90);
        Future<User> queue2 = helloService.getUserById(91);
        Future<User> queue3 = helloService.getUserById(92);
        User u1 = queue1.get();
        User u2 = queue2.get();
        User u3=  queue3.get();
        System.out.println(u1);
        System.out.println(u2);
        System.out.println(u3);
        c.close();//结束初始化
    }
}
